package excerciseEight1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import org.omg.CORBA.PRIVATE_MEMBER;

public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		ArrayList<String> str=new ArrayList<String>();
		String a;
		int count=0;
		do{
			a=in.next();
			boolean isTrue;
			if(a.equals("###"))break;
			else{
				str.add(a);
				count++;
			}
		}while(!a.equals("###"));
		
		City[] array=new City[count];
		for(int i=0;i<count;i++){
			array[i]=new City(str.get(i));
		}
		int num=0;
		Integer b;
		while(num<count*count){
			b=in.nextInt();
			num++;
			int c=(num-1)%count;
			array[c].list.add(b);
		}
		String city1,city2;
		city1=in.next();
		city2=in.next();
		int result=array[str.indexOf(city1)].list.get(str.indexOf(city2));
		System.out.print(result);
	}

}

class City{
	String name;
	ArrayList<Integer> list=new ArrayList<>();
	City(String str){
		name=str;
	}
}